package com.smart.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.pojo.Product;
import com.smart.pojo.ResultBean;
import com.smart.req.PageData;
import com.smart.req.ProductReq;
import com.smart.service.IProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lyj
 * @since 2023-05-13
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     * 根据条件分页查询产品列表
     *
     * @param productReq
     * @return
     */
    @GetMapping("queryByCondition")
    public ResultBean queryByCondition(ProductReq productReq) {
        System.out.println("查询产品的关键词：" + productReq);
        Page<Product> page = new Page<>(productReq.getPageNum(), productReq.getPageSize());
        productService.queryByCondition(productReq, page);
        return ResultBean.success(page);
    }

    @GetMapping("queryActiveProduct")
    public ResultBean queryActiveProduct() {
        System.out.println("获取所有已上架处于正常状态的产品");
        return ResultBean.success(productService.lambdaQuery().eq(Product::getStatus, 0).list());
    }

    @GetMapping("queryAll")
    public ResultBean queryAll() {
        System.out.println("获取所有已上架处于正常状态的产品");
        return ResultBean.success(productService.list());
    }

    @GetMapping("queryById/{id}")
    public ResultBean queryById(@PathVariable("id") Long id) {
        return ResultBean.success(productService.getById(id));
    }

    /**
     * 上架产品或修改产品信息
     *
     * @param product
     * @return
     */
    @PostMapping("saveOrUpdate")
    @RequiresPermissions({"product:save"})
    public ResultBean saveOrUpdate(@RequestBody Product product) {
        System.out.println("新增或修改的产品信息：" + product);
        boolean rs = productService.saveOrUpdateProduct(product);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    /**
     * 根据产品id删除产品信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("deleteById/{id}")
    @RequiresPermissions({"product:del"})
    public ResultBean deleteById(@PathVariable("id") Long id) {
        System.out.println("要删除的产品的ID：" + id);
        boolean rs = productService.deleteById(id);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    /**
     * 获取无生产任务的产品集合
     */
    @GetMapping("queryNotProducePlan")
    public ResultBean queryNotProducePlan() {
        System.out.println("获取无生产任务的产品集合");
        List<Product> productList = productService.queryNotProducePlan();
        return ResultBean.success(productList);
    }
}
